﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace HYFloorPipeModule
{
	public partial class HYCalculateHeatForm : System.Windows.Forms.Form
	{
		public HYCalculateHeatForm(Autodesk.Revit.DB.Document doc, Selection sel)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			this.sel = sel;
			base.Icon = Resource.YArchitechIcon;
			this.sqlHelper = new SQLiteDataBaseHelper(Path.Combine(Product.DataLocation, "HYBIMSPACE.db3"));
			this.LoadCalculateHeatSetting();
			this.Calculate();
		}

		private void LoadCalculateHeatSetting()
		{
			object[] items = new object[]
			{
				"聚苯乙烯",
				"发泡水泥"
			};
			this.cbHeatLayer.Items.AddRange(items);
			object[] items2 = new object[]
			{
				"水泥或陶瓷",
				"木地板",
				"厚地毯",
				"塑料"
			};
			this.cbGroundLayer.Items.AddRange(items2);
			object[] items3 = new object[]
			{
				"PB",
				"PE-X"
			};
			this.cbHeatPipeType.Items.AddRange(items3);
			object[] items4 = new object[]
			{
				"35",
				"40",
				"45",
				"50",
				"55"
			};
			this.cbAverageWater.Items.AddRange(items4);
			object[] items5 = new object[]
			{
				"16",
				"18",
				"20",
				"22",
				"24"
			};
			this.cbIndoorAirTemperature.Items.AddRange(items5);
			Dictionary<string, string> calculateHeatParam = this.userSetting.GetCalculateHeatParam();
			this.cbHeatLayer.SelectedItem = this.userSetting.GetValueFromDic("heatLayer", "聚苯乙烯", calculateHeatParam);
			this.cbGroundLayer.SelectedItem = this.userSetting.GetValueFromDic("groundLayer", "水泥或陶瓷", calculateHeatParam);
			this.cbHeatPipeType.SelectedItem = this.userSetting.GetValueFromDic("heatPipeType", "PB", calculateHeatParam);
			this.cbAverageWater.SelectedItem = this.userSetting.GetValueFromDic("averageWater", "40", calculateHeatParam);
			this.cbIndoorAirTemperature.SelectedItem = this.userSetting.GetValueFromDic("indoorAirTemperature", "18", calculateHeatParam);
			this.hyTxtHeatPipeDistance.Text = this.userSetting.GetValueFromDic("heatPipeDistance", "300", calculateHeatParam);
			this.hyTxtLayArea.Text = this.userSetting.GetValueFromDic("layArea", "16", calculateHeatParam);
			this.hyTxtTotalHeat.Text = this.userSetting.GetValueFromDic("totalHeat", "1900", calculateHeatParam);
			if (int.Parse(this.userSetting.GetValueFromDic("calculateType", "0", calculateHeatParam)) == 0)
			{
				this.rdoHeat.Checked = true;
			}
			else
			{
				this.rdoDistance.Checked = true;
			}
			if (this.rdoHeat.Checked)
			{
				this.hyTxtTotalHeat.Enabled = false;
				this.hyTxtHeatPipeDistance.Enabled = true;
			}
			else
			{
				this.hyTxtTotalHeat.Enabled = true;
				this.hyTxtHeatPipeDistance.Enabled = false;
			}
			this.toolTipArea.SetToolTip(this.btnArea, "返回Revit图面，拾取房间面积");
			this.toolTipTemp.SetToolTip(this.btnTemperature, "查看地暖技术规程要求");
		}

		private void SaveCalculateHeatSetting()
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("heatLayer", this.cbHeatLayer.Text);
			dictionary.Add("groundLayer", this.cbGroundLayer.Text);
			dictionary.Add("heatPipeType", this.cbHeatPipeType.Text);
			dictionary.Add("averageWater", this.cbAverageWater.Text);
			dictionary.Add("indoorAirTemperature", this.cbIndoorAirTemperature.Text);
			dictionary.Add("heatPipeDistance", this.hyTxtHeatPipeDistance.Text);
			dictionary.Add("layArea", this.hyTxtLayArea.Text);
			dictionary.Add("totalHeat", this.hyTxtTotalHeat.Text);
			int num;
			if (this.rdoHeat.Checked)
			{
				num = 0;
			}
			else
			{
				num = 1;
			}
			dictionary.Add("calculateType", num.ToString());
			this.userSetting.WriteCalculateHeatParam(dictionary);
		}

		private void btnCalculate_Click(object sender, EventArgs e)
		{
			this.Calculate();
			this.SaveCalculateHeatSetting();
		}

		private void Calculate()
		{
			double num = double.Parse(this.cbIndoorAirTemperature.Text);
			double num3;
			if (this.rdoHeat.Checked)
			{
				if (this.hyTxtHeatPipeDistance.Text.Trim() == "" || this.hyTxtLayArea.Text.Trim() == "")
				{
					YJKMessageBox.Information("计算参数不能为空");
					return;
				}
				if (this.cbHeatPipeType.SelectedIndex == 2)
				{
					if (double.Parse(this.hyTxtHeatPipeDistance.Text) < 200.0 || double.Parse(this.hyTxtHeatPipeDistance.Text) > 500.0)
					{
						YJKMessageBox.Information("铜管加热管间距应在200~500之间");
						return;
					}
				}
				else if (double.Parse(this.hyTxtHeatPipeDistance.Text) < 100.0 || double.Parse(this.hyTxtHeatPipeDistance.Text) > 500.0)
				{
					YJKMessageBox.Information("加热管间距应在100~500之间");
					return;
				}
				double num2 = this.GetUnitAreaHeat();
				num3 = num + 9.82 * Math.Pow(num2 / 100.0, 0.969);
				double num4 = num2 * double.Parse(this.hyTxtLayArea.Text);
				this.hyTxtSurfaceTemperature.Text = num3.ToString("0.0");
				this.hyTxtCoilDistance.Text = this.hyTxtHeatPipeDistance.Text;
				this.hyTxtUnitAreaHeat.Text = num2.ToString("0.00");
				this.hyTxtResultTotalHeat.Text = num4.ToString("0.00");
				if (this.hyTxtResultTotalHeat.Text.Trim() != "")
				{
					this.hyTxtTotalHeat.Text = this.hyTxtResultTotalHeat.Text;
				}
			}
			else
			{
				if (this.hyTxtTotalHeat.Text.Trim() == "" || this.hyTxtLayArea.Text.Trim() == "")
				{
					YJKMessageBox.Information("计算参数不能为空");
					return;
				}
				double num2 = double.Parse(this.hyTxtTotalHeat.Text) / double.Parse(this.hyTxtLayArea.Text);
				num3 = num + 9.82 * Math.Pow(num2 / 100.0, 0.969);
				double coilDistance = this.GetCoilDistance(double.Parse(num2.ToString("0.00")));
				if (coilDistance == -1.0)
				{
					return;
				}
				this.hyTxtCoilDistance.Text = coilDistance.ToString("0");
				this.hyTxtSurfaceTemperature.Text = num3.ToString("0.0");
				this.hyTxtUnitAreaHeat.Text = num2.ToString("0.00");
				this.hyTxtResultTotalHeat.Text = double.Parse(this.hyTxtTotalHeat.Text).ToString("0.00");
				if (this.hyTxtCoilDistance.Text.Trim() != "")
				{
					this.hyTxtHeatPipeDistance.Text = this.hyTxtCoilDistance.Text;
				}
			}
			if (num3 < 25.0)
			{
				this.lbTemperature.Text = "注：地面温度偏低";
				return;
			}
			if (num3 > 32.0)
			{
				this.lbTemperature.Text = "注：地面温度偏高";
				return;
			}
			this.lbTemperature.Text = "注：地面温度正常";
		}

		private double GetUnitAreaHeat()
		{
			double result;
			try
			{
				double num = double.Parse(this.hyTxtHeatPipeDistance.Text.Substring(0, 1)) * 100.0;
				double num2 = double.Parse(this.hyTxtHeatPipeDistance.Text.Substring(1, 2)) / 100.0;
				DataTable dataTable = SQLiteDataOperate.ExecQuery(string.Concat(new string[]
				{
					"select * from ClrfRdt where AverageWaterTemperature='",
					this.cbAverageWater.Text,
					"'and IndoorAirTemperature='",
					this.cbIndoorAirTemperature.Text,
					"'and Lagging='",
					this.cbHeatLayer.Text,
					"'and FloorMedium='",
					this.cbGroundLayer.Text,
					"'and PIPE='",
					this.cbHeatPipeType.Text,
					"'"
				}));
				double num5;
				if (num != 500.0)
				{
					double num3 = double.Parse(dataTable.Rows[0]["DIST" + num + "ClrfRdt"].ToString());
					double num4 = double.Parse(dataTable.Rows[0]["DIST" + (num + 100.0) + "ClrfRdt"].ToString());
					num5 = num3 + (num4 - num3) * num2;
				}
				else
				{
					num5 = double.Parse(dataTable.Rows[0]["DIST" + num + "ClrfRdt"].ToString());
				}
				result = num5;
			}
			catch (Exception)
			{
				result = 0.0;
			}
			return result;
		}

		private double GetCoilDistance(double dUnitAreaHeat)
		{
			double num = double.Parse(this.hyTxtTotalHeat.Text);
			double result = 0.0;
			DataTable dataTable = SQLiteDataOperate.ExecQuery(string.Concat(new string[]
			{
				"select * from ClrfRdt where AverageWaterTemperature='",
				this.cbAverageWater.Text,
				"'and IndoorAirTemperature='",
				this.cbIndoorAirTemperature.Text,
				"'and Lagging='",
				this.cbHeatLayer.Text,
				"'and FloorMedium='",
				this.cbGroundLayer.Text,
				"'and PIPE='",
				this.cbHeatPipeType.Text,
				"'"
			}));
			double num2 = double.Parse(double.Parse(dataTable.Rows[0]["DIST100ClrfRdt"].ToString()).ToString("0.00"));
			double num3 = double.Parse(double.Parse(dataTable.Rows[0]["DIST200ClrfRdt"].ToString()).ToString("0.00"));
			double num4 = double.Parse(double.Parse(dataTable.Rows[0]["DIST300ClrfRdt"].ToString()).ToString("0.00"));
			double num5 = double.Parse(double.Parse(dataTable.Rows[0]["DIST400ClrfRdt"].ToString()).ToString("0.00"));
			double num6 = double.Parse(double.Parse(dataTable.Rows[0]["DIST500ClrfRdt"].ToString()).ToString("0.00"));
			if (this.cbHeatPipeType.SelectedIndex == 2)
			{
				if (dUnitAreaHeat > num3 || dUnitAreaHeat < num6)
				{
					this.lbTemperature.Text = string.Concat(new string[]
					{
						"计算失败：盘管布置面积\r\n 应在",
						(num / num3).ToString("0.00"),
						"~",
						(num / num6).ToString("0.00"),
						"之间。"
					});
					return -1.0;
				}
				if (dUnitAreaHeat <= num3 && dUnitAreaHeat >= num4)
				{
					result = 200.0 + (num3 - dUnitAreaHeat) / (num3 - num4) * 100.0;
				}
				else if (dUnitAreaHeat <= num4 && dUnitAreaHeat >= num5)
				{
					result = 300.0 + (num4 - dUnitAreaHeat) / (num4 - num5) * 100.0;
				}
				else if (dUnitAreaHeat <= num5 && dUnitAreaHeat >= num6)
				{
					result = 400.0 + (num5 - dUnitAreaHeat) / (num5 - num6) * 100.0;
				}
			}
			else
			{
				if (dUnitAreaHeat > num2 || dUnitAreaHeat < num6)
				{
					this.lbTemperature.Text = string.Concat(new string[]
					{
						"计算失败：盘管布置面积\r\n 应在",
						(num / num2).ToString("0.00"),
						"~",
						(num / num6).ToString("0.00"),
						"之间。"
					});
					return -1.0;
				}
				if (dUnitAreaHeat <= num2 && dUnitAreaHeat >= num3)
				{
					result = 100.0 + (num2 - dUnitAreaHeat) / (num2 - num3) * 100.0;
				}
				else if (dUnitAreaHeat <= num3 && dUnitAreaHeat >= num4)
				{
					result = 200.0 + (num3 - dUnitAreaHeat) / (num3 - num4) * 100.0;
				}
				else if (dUnitAreaHeat <= num4 && dUnitAreaHeat >= num5)
				{
					result = 300.0 + (num4 - dUnitAreaHeat) / (num4 - num5) * 100.0;
				}
				else if (dUnitAreaHeat <= num5 && dUnitAreaHeat >= num6)
				{
					result = 400.0 + (num5 - dUnitAreaHeat) / (num5 - num6) * 100.0;
				}
			}
			return result;
		}

		private void rdoHeat_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoHeat.Checked)
			{
				this.hyTxtTotalHeat.Enabled = false;
				this.hyTxtHeatPipeDistance.Enabled = true;
				return;
			}
			this.hyTxtTotalHeat.Enabled = true;
			this.hyTxtHeatPipeDistance.Enabled = false;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void btnRules_Click(object sender, EventArgs e)
		{
			new HYHeatParamForm(this.cbHeatPipeType.Text, this.cbHeatLayer.Text, this.cbGroundLayer.Text)
			{
				Text = string.Concat(new string[]
				{
					this.cbHeatPipeType.Text,
					"管单位面积散热量和传热损失[隔热层为 ",
					this.cbHeatLayer.Text,
					";地面层为 ",
					this.cbGroundLayer.Text,
					"]"
				})
			}.ShowDialog();
		}

		private void btnTemperature_Click(object sender, EventArgs e)
		{
			new HYSurfaceTemperatureForm().ShowDialog();
		}

		private void btnArea_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Abort;
		}

		public void GetRoomArea()
		{
			try
			{
				ISelectionFilter selectionFilter = new HYRoomFilter();
				Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "请选择一个房间");
				this.roomElem = this.doc.GetElement(reference);
				double num = this.roomElem.GetParameter(BuiltInParameter.ROOM_AREA).AsDouble() * 0.3048 * 0.3048;
				this.hyTxtLayArea.Text = num.ToString("0.00");
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
			}
		}

		private void btnDrawCoil_Click(object sender, EventArgs e)
		{
			if (this.hyTxtCoilDistance.Text == "")
			{
				YJKMessageBox.Information("没有计算数据，或者计算失败，不能进行绘制！");
				return;
			}
			this.strPipeType = this.cbHeatPipeType.Text;
			this.strPipeDistance = this.hyTxtCoilDistance.Text;
			this.strType = "矩形";
			base.DialogResult = DialogResult.OK;
		}

		private void btnDrawYX_Click(object sender, EventArgs e)
		{
			if (this.hyTxtCoilDistance.Text == "")
			{
				YJKMessageBox.Information("没有计算数据，或者计算失败，不能进行绘制！");
				return;
			}
			this.strPipeType = this.cbHeatPipeType.Text;
			this.strPipeDistance = this.hyTxtCoilDistance.Text;
			this.strType = "异形";
			base.DialogResult = DialogResult.OK;
		}

		private void cbHeatLayer_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cbHeatLayer.SelectedIndex == 0)
			{
				if (!this.cbHeatPipeType.Items.Contains("铜管"))
				{
					this.cbHeatPipeType.Items.Add("铜管");
					return;
				}
			}
			else
			{
				this.cbHeatPipeType.Items.Remove("铜管");
				if (this.cbHeatPipeType.SelectedItem == null)
				{
					this.cbHeatPipeType.SelectedIndex = 0;
				}
			}
		}

		private void hyTxtLayArea_Leave(object sender, EventArgs e)
		{
			this.roomElem = null;
		}

		private SQLiteDataBaseHelper sqlHelper;

		private HVACUserSetting userSetting = new HVACUserSetting();

		private Document doc;

		private Selection sel;

		public Element roomElem;

		public string strRoomArea = "";

		public string strPipeType = "";

		public string strPipeDistance = "";

		public string strType = "";
	}
}
